<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>基本资料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <link rel="stylesheet" href="/static/lib/send-code/yiui2.css" media="all">
    <script src="/static/lib/send-code/smscodeinput.js"></script>
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<style>
    #coverImg{
        height: 350px;
        width: 350px;
        display: none;
    }
    #lookCover{
        display: none;
    }
    #subbtn{
        width: 170px;
    }
    #edit{
        position: absolute;
        top: 0;
        right: 0;
    }
    .layui-form-item{
        margin-bottom: 22px;
    }
    .layui-layer-btn a{
        font-size: 12px;
    }
    .layui-layout-admin .layui-body{
        padding-bottom: 0;
    }

</style>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form id="addForm" class="layui-form layui-form-pane">
            <input type="hidden" id="managerId" name="managerId">
            <div class="layui-form-item">
                <label class="layui-form-label required">昵称</label>
                <div class="layui-input-block">
                    <input type="text" id="managerName" name="managerName" lay-verify="required" lay-reqtext="管理账号不能为空"   class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-block">
                    <input type="radio" id="man"  name="managerGender" value="男" title="男">
                    <input type="radio" id="woman" name="managerGender" value="女" title="女">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">手机号</label>
                <div class="layui-input-block">
                    <input type="number" readonly id="managerPhone" name="managerPhone" lay-verify="required" lay-reqtext="手机不能为空"    class="layui-input"/>
                    <a id="edit" class="layui-btn layui-btn-normal">更换号码</a>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">身份证号</label>
                <div class="layui-input-block">
                    <input type="text" id="idCard" lay-verify="required|identity" name="idCard" class="layui-input"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">头像</label>
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="uploadImg">上传头像</button>
                    <button type="button" class="layui-btn layui-btn-normal" id="lookCover">查看头像</button>
                    <img class="layui-upload-img"  id="coverImg" alt="头像" src="">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="margin: 0;text-align: center">
                    <button id="subbtn" class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">保存信息</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script th:inline="javascript">
    let data={
        managerId: null,
        managerName:null,
        managerPhone: null,
        idCard:null,
        avatarUrl: null,
        managerGender:null
    }
    let url;        //添加/修改头像路径
    let imgUrl;     //讲师头像路径
    let layer;
    layui.use(['form','miniTab','upload'], function () {
        layer = layui.layer;
        let form = layui.form,
            upload = layui.upload,
            miniTab = layui.miniTab;

        //常规使用 - 普通图片上传
        let uploadInst = upload.render({
            elem: '#uploadImg',
            url: '/upload/avatar', //改成您自己的上传接口
            before: function (e) {
                this.data = {
                    dirName: 'manager',
                }
                layer.msg('上传中', {icon: 16, time: 0});
            },
            done: function (res) {
                //如果上传失败
                if (res.code ===200) {
                    url = res.data.url
                    imgUrl=res.data.url
                    $('#coverImg').attr('src', url); //图片链接
                    $('#lookCover').css("display","inline");
                    $('#lookCover').click();
                    return layer.msg('上传成功');
                }else{
                    return layer.msg(res.message);
                }
            },
            error: function () {
                return layer.msg('上传失败');
            }
        });

    });
    $(function (){
        let manager=[[${manager}]];
        $('#managerId').val(manager.managerId);
        $('#managerName').val(manager.managerName);
        $('#managerPhone').val(manager.managerPhone);
        $('#idCard').val(manager.idCard);

        if(manager.avatarUrl!==null && manager.avatarUrl!==''){
            $('#lookCover').css("display","inline");
            imgUrl=manager.avatarUrl;
            url=manager.avatarUrl;
        }
        if(manager.managerGender!==null && manager.managerGender!==''){
            if(manager.managerGender==='女'){
                $('#woman').prop("checked","checked");
            }else{
                $('#man').prop("checked","checked");
            }
        }
        layui.form.render(); //重新渲染显示效果

        //弹出封面
        $("#lookCover").click(function () {
            $('#coverImg').attr('src',imgUrl);
            layer.open({
                type: 1,
                title: false,
                closeBtn: 1,
                area: ['auto'],
                skin: 'layui-layer-nobg', //没有背景色
                shadeClose: true,
                content: $('#coverImg'),
                end:function(){
                    $('#coverImg').css("display","none");
                }
            });
        });

        //提交表单
        $('#addForm').submit(function (e) {
            let formData=new FormData(this);
            data.managerId=formData.get("managerId");
            data.managerName=formData.get("managerName");
            data.managerPhone=formData.get("managerPhone");
            data.idCard=formData.get("idCard");
            data.avatarUrl=url;
            data.managerGender=formData.get("managerGender");
            $.ajax({
                type:"post",
                url:'/managerInfo/editManagerInfo',
                data:data,
                success:function (res){
                    if(res.code===200){
                        layer.msg(res.message,{time:2000,icon:1,offset:[15]});
                        let index=parent.layer.getFrameIndex(window.name);
                        setTimeout(function (){
                            window.parent.location.reload();//刷新父页面
                            parent.layer.close(index);
                        },500);
                    }else{
                        layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                    }
                },
                error:function (error){
                    layer.msg(error,{time:5000,icon:2,offset:[15]});
                }
            })
            e.preventDefault();
        });
        //编辑弹窗
        $('#edit').click(function (){
            layer.prompt({title:'输入手机号，并确认',btn:'发送验证码',formType:0},function (phoneNumber,index){
               layer.close(index);
               let load = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
                $.ajax({
                    type:"post",
                    url:'/sms/phoneCheckCode',
                    data:{
                        phone:phoneNumber
                    },
                    success:function (res){
                        if(res.code===200){
                           layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                            let scbox = yiuiSmsCodeBox({
                                text:'验证码已发送到您填写的手机号码上，请注意查收',//可选
                                maxLength:6,//可选，默认为4
                                submit:function(checkCode){ // 可选
                                    //按确定时回调
                                    $.ajax({
                                        type: "post",
                                        url: '/util/check/phone',
                                        data: {
                                            phone: phoneNumber,
                                            checkCode: checkCode
                                        },
                                        success: function (res) {
                                            $('#managerPhone').val(phoneNumber);
                                            layer.msg("手机号更换成功，请保存信息",{time:2000,icon:1,offset:[15]});
                                            layer.close(index);
                                        }
                                    });
                                    scbox.destory();//销毁
                                },
                            });
                        }else{
                            layer.msg(res.message,{time:5000,icon:1,offset:[15]});
                        }
                    },
                    error:function (error){
                        layer.msg(error,{time:5000,icon:2,offset:[15]});
                    },
                    complete:function () {
                        layer.close(load)
                    }
                })
                layer.form.render();
            layer.renderer();
            })
        })
    })
</script>
</body>
</html>